package com.team.service.impl;

import com.team.dao.AdminMapper;
import com.team.entity.Admin;
import com.team.service.LoginService;
import com.team.util.WebUtil;
import com.team.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author WHZ
 * @date 2022年10月15日 10:35
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 根据管理员账号和密码登陆
     * @param name
     * @param password
     * @return
     */
    @Override
    public Result disembarkByAdmin(String name, String password) {
        Admin admin = adminMapper.selectByadmin(name, password);
        if (admin!=null){
            WebUtil.getSession().setAttribute("admin",admin);
            System.out.println(admin);
            return new Result(200,"登陆成功");
        }else {
            return new Result(500,"登陆失败");
        }
    }

    @Override
    public Result getLoginAdmin() {
        Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");
        Admin loginAdmin = adminMapper.selectByPrimaryKey(admin.getId());
        return new Result(200,"登录成功",loginAdmin);
    }

    @Override
    public Result quit() {
        WebUtil.getSession().removeAttribute("admin");
        return new Result(200,"退出用户");
    }
}
